namespace AdminApiTemplate.Common.Helpers
{
    /// <summary>
    /// 文件处理
    /// </summary>
    public class FileHelper
    {
        /// <summary>
        /// 保存文件
        /// </summary>
        /// <param name="bytes"></param>
        /// <param name="module"></param>
        /// <param name="sourceFileName"></param>
        /// <param name="fileName"></param>
        public static void SaveToFile(byte[] bytes, string module, string sourceFileName, ref string fileName)
        {
            var folder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Uploads", module);
            if (!Directory.Exists(folder))
            {
                Directory.CreateDirectory(folder);
            }
            var fileExt = Path.GetExtension(sourceFileName);
            fileName = RandomHelper.RequestId() + fileExt;
            var fullName = Path.Combine(folder, fileName);
            using (var stream = new FileStream(fullName, FileMode.OpenOrCreate))
            {
                stream.Write(bytes);
                stream.Flush();
            }
        }
    }
}
